package br.unifor.cadeventos.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import br.unifor.cadeventos.entity.Eventos;
import br.unifor.cadeventos.internacionalizacao.Messagens;
import br.unifor.cadeventos.manager.FiltroEventoManager;
import br.unifor.cadeventos.utils.DesktopContext;

public class FiltroEvento extends JPanel {
	
	private static final long serialVersionUID = 6160869189227266073L;
	
	private JTextField textField;
	private JTable table;
	private static JComboBox<String> comboBox;
	private static JLabel lblDescricao;
	private FiltroEventoManager feManager = new FiltroEventoManager();
	private static JLabel labelAviso = new JLabel("Nenhum dado encontrado!");
	private static JScrollPane scrollPane;

	/**
	 * Create the panel.
	 */
	public FiltroEvento() {
		setLayout(null);
		
		DesktopContext.getContextFrame().setTitle("Selecionar Eventos");
		
		JLabel lblFiltro = new JLabel("Filtro*:");
		lblFiltro.setBounds(10, 51, 61, 16);
		add(lblFiltro);

		lblDescricao = new JLabel("Título*:");
		lblDescricao.setBounds(143, 51, 50, 16);
		add(lblDescricao);
		
		textField = new JTextField();
		textField.setBounds(183, 45, 235, 28);
		add(textField);
		textField.setColumns(10);
		
		JButton btnBuscar = new JButton("Buscar");
		btnBuscar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(!textField.getText().trim().equals("")){
					if(comboBox.getSelectedIndex() == 0) {
						feManager.buscaEventosPorTitulo(textField.getText());
					} else {
						feManager.buscaEventosPorLocal(textField.getText());
					}
					montaTabela();
					repaint();
				} else {
					JOptionPane.showMessageDialog(null, Messagens.MSG_CMP_OBR);
				}
			}

			private void montaTabela() {
				
				if(labelAviso != null) {
					remove(labelAviso);
				}
				String[] colunas = {"Título", "Local", "Data Início", "Data Fim"};
				List<Eventos> eventos = feManager.getEventos();
				String[][] dados = new String[5][4];
				if(!eventos.isEmpty()) {
					SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
					for (int i = 0; i < eventos.size(); i++) {
						dados[i][0] = eventos.get(i).getTitulo();
						dados[i][1] = eventos.get(i).getLocalEvento();
						dados[i][2] = sdf.format(eventos.get(i).getDataInicio());
						dados[i][3] = sdf.format(eventos.get(i).getDataFim());
					}
					DefaultTableModel modelo = new DefaultTableModel(dados, colunas);
					table = new JTable(modelo);
					scrollPane = new JScrollPane(table);
					scrollPane.setBounds(26, 147, 600, 154);
				    add(scrollPane, BorderLayout.CENTER);
				} else {
					if(scrollPane != null) {
						remove(scrollPane);
					}
					labelAviso.setBounds(26, 247, 600, 154);
					add(labelAviso);
				}
				repaint();
				
			}
		});
		btnBuscar.setBounds(428, 45, 96, 29);
		add(btnBuscar);
		
		comboBox = new JComboBox<String>();
		comboBox.setBounds(52, 46, 76, 27);
		comboBox.addItem(Messagens.COMBOX_TITULO_FILTROEVENTO);
		comboBox.addItem(Messagens.COMBOX_LOCAL_FILTROEVENTO);
		comboBox.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				String valor = (String)(comboBox.getSelectedItem());
				lblDescricao.setText(valor+":");
				repaint();
			}
		});
		add(comboBox);
		
		JButton btnInserir = new JButton("Inserir");
		btnInserir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				FormEventos frmEventos = new FormEventos();				

		        DesktopContext.getContextFrame().setContentPane(frmEventos);
		        DesktopContext.getContextFrame().setVisible(true);
			}
		});
		btnInserir.setBounds(534, 45, 96, 29);
		add(btnInserir);
	}
}
